package cn.selfiot.iotman.helper;

import com.panemu.tiwulfx.dialog.MessageDialog;
import com.panemu.tiwulfx.dialog.MessageDialogBuilder;
import javafx.stage.Stage;

/**
 * 警告框
 *
 * @author hugo
 * @since 2020/8/1
 */
public class MessageDialogHelper {
    public static MessageDialog.Answer showDialog(Stage stage,
                                                  String alertType,
                                                  String message,
                                                  Throwable throwable,
                                                  MessageDialog.ButtonType buttonType,
                                                  String... btnTxt) {
        MessageDialogBuilder dialogBuilder = MessageDialogBuilder.info();
        
        if (throwable != null) {
            return MessageDialogBuilder
                    .error(throwable)
                    .message(message)
                    .show(stage.getScene().getWindow());
        }
        
        if ("confirmation".equalsIgnoreCase(alertType)) {
            return MessageDialogBuilder
                    .confirmation()
                    .message(message)
                    .show(stage.getScene().getWindow());
        }
        
        if ("info".equalsIgnoreCase(alertType)) {
            dialogBuilder = MessageDialogBuilder.info();
        }
        if ("warning".equalsIgnoreCase(alertType)) {
            dialogBuilder = MessageDialogBuilder.warning();
        }
        if ("error".equalsIgnoreCase(alertType)) {
            dialogBuilder = MessageDialogBuilder.error();
        }
        
        dialogBuilder = dialogBuilder.message(message)
                .buttonType(buttonType);
        if (buttonType.equals(MessageDialog.ButtonType.OK)) {
            dialogBuilder = dialogBuilder.yesOkButtonText(btnTxt[0]);
        }
        if (buttonType.equals(MessageDialog.ButtonType.YES_NO)) {
            dialogBuilder = dialogBuilder.yesOkButtonText(btnTxt[0])
                    .noButtonText(btnTxt[1]);
        }
        if (buttonType.equals(MessageDialog.ButtonType.YES_NO_CANCEL)) {
            dialogBuilder = dialogBuilder.yesOkButtonText(btnTxt[0])
                    .noButtonText(btnTxt[1])
                    .cancelButtonText(btnTxt[2]);
        }
        
        return dialogBuilder.show(stage.getScene().getWindow());
    }
    
    public static MessageDialog.Answer confirm(Stage stage, String message) {
        return showDialog(stage, "confirmation",
                message, null,
                null, null, null, null);
    }
    
    public static MessageDialog.Answer showExceptionDialog(Stage stage,
                                                           Throwable throwable) {
        return showDialog(stage, null, "Throw exception ...",
                throwable, null, null, null, null);
    }
}
